<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户组模型类
 */
class User_broker_model extends CI_Model
{
	/**
	 * 获取经纪人信息
	 */
	public function get($uid)
	{
		$uid = intval($uid);
		$row = $this->db->get_where('user_broker', array('uid' => $uid))->row_array();
		return $row;
	}

	/**
	 * 更新经纪人资料
	 */
	public function update($data)
	{
		if ($data['uid'])
		{
			$this->db->where('uid', $data['uid']);
			//$this->db->limit(1);
			$this->db->update('user_broker', $data);
		}
	}

    /**
	 * 获取所有经纪人信息（分页显示）
	 *
	 * @param $where array
	 * @param $limit int
	 * @param $offset int
	 * @param $orderby string ('uid DESC, created ASC')
	 * @return array
	 */
	public function get_all_list($where = FALSE, $limit = FALSE, $offset = FALSE, $orderby = FALSE)
	{
		$this->_where_list($where);
		$this->db->order_by($orderby);
		$this->db->limit($limit, $offset);
		$result = $this->db->get('user_broker')->result_array();
		return $result;
	}

	public function count_all_list($where)
	{
		$this->_where_list($where);
		return $this->db->count_all_results('user_broker');
	}

	private function _where_list($where)
	{
		//var_dump($where['groupid']);
		if (isset($where['groupid']) && $where['groupid'] != -1 && $where['groupid'] != FALSE)
		{
			$groupid = intval($where['groupid']);
			//普通用户组
			if ($groupid == 0)
			{
				//$this->db->where_in('groupid', array(8, 9));
			}
			else
			{
				$this->db->where('groupid', $groupid);
			}
		}
		if (isset($where['uid']) && $where['uid'] != FALSE)
		{
			$uid = intval($where['uid']);
			$this->db->where('uid', $uid);
		}
		$side = 'after'; //默认 key%
		if (isset($where['like']) && $where['like'] != FALSE) //模糊查询 %key%
		{
			$side = $where['like'];
		}
		if (isset($where['username']) && $where['username'] != FALSE)
		{
			$username = $where['username'];
			$this->db->like('username', $username, $side);
		}
		if (isset($where['email']) && $where['email'] != FALSE)
		{
			$email = $where['email'];
			$this->db->like('email', $email, $side);
		}
	}

    public function lock($uid)
	{
		$this->db->update('user_broker', array('status' => 'lock'), array('uid' => $uid));
	}

	public function unlock($uid)
	{
		$this->db->update('user_broker', array('status' => 'active'), array('uid' => $uid));
	}

	public function active($uid)
	{
		$this->unlock($uid);
	}
}

/* End of file user_broker_model.php */
/* Location: ./application/models/user_broker_model.php */
